package br.edu.ufcg.dsc.domain.resource;

import java.util.ArrayList;
import java.util.List;

import br.edu.ufcg.dsc.domain.marc.MARCResource;

public class Article {

	private String isbn, cutter, title, creator, contributor, description, source, format, date;

	private List<String> subjects;
	
	
	public Article(){
		subjects = new ArrayList<String>();
	}
	
	public Article(MARCResource marc){
		setIsbn(getAttribute(marc, "020", "a"));
		//nao encontrei.
		setCutter(getAttribute(marc, "XXX", "X"));
		setTitle(getAttribute(marc, "245", "a"));
		setCreator(getAttribute(marc, "720", "a"));
		setContributor(getAttribute(marc, "720", "a"));
		subjects = getMultipleAttributes(marc, "650", "a");
		setDescription(getAttribute(marc, "520", "a"));
		setSource(getAttribute(marc, "786", "a"));
		setFormat(getAttribute(marc, "856", "q"));
		setDate(getAttribute(marc, "260", "c"));
	}

	
	private List<String> getMultipleAttributes(MARCResource marc,
			String field, String subfield) {

		List<String> result = new ArrayList<String>();
		
		for (String key : marc.getDataFields().get(field).getSubfields().keySet())
			if (key.startsWith(subfield))
				result.add(marc.getDataFields().get(field).getSubfields().get(key));
		
		return result;
	}

	private String getAttribute(MARCResource marc, String field, String subfield) {
		for (String key : marc.getDataFields().get(field).getSubfields().keySet())
			if (key.startsWith(subfield))
				return marc.getDataFields().get(field).getSubfields().get(key);

		return "";
	}
	
	public String getIsbn() {
		return isbn;
	}

	public void setIsbn(String isbn) {
		this.isbn = isbn;
	}

	public String getCutter() {
		return cutter;
	}

	public void setCutter(String cutter) {
		this.cutter = cutter;
	}

	public String getTitle() {
		return title;
	}

	public void setTitle(String title) {
		this.title = title;
	}

	public String getCreator() {
		return creator;
	}

	public void setCreator(String creator) {
		this.creator = creator;
	}

	public String getContributor() {
		return contributor;
	}

	public void setContributor(String contributor) {
		this.contributor = contributor;
	}

	public List<String> getSubjects() {
		return subjects;
	}

	public void setSubjects(List<String> subjects) {
		this.subjects = subjects;
	}

	public String getDescription() {
		return description;
	}

	public void setDescription(String description) {
		this.description = description;
	}

	public String getSource() {
		return source;
	}

	public void setSource(String source) {
		this.source = source;
	}

	public String getFormat() {
		return format;
	}

	public void setFormat(String format) {
		this.format = format;
	}

	public String getDate() {
		return date;
	}

	public void setDate(String date) {
		this.date = date;
	}
	
	
	
}
